package com.ext.aic.utils;

import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable;
import android.util.Size;
import android.util.SizeF;
import android.util.SparseArray;

import com.eyinfo.android_pure_utils.utils.ObjectJudge;

import java.io.Serializable;
import java.util.ArrayList;

public class BundleUtils {
    /**
     * 设置bundle值
     *
     * @param bundle bundle
     * @param key    键
     * @param value  值
     */
    public static void setBundleValue(Bundle bundle, String key, Object value) {
        if (value instanceof Boolean) {
            //boolean
            bundle.putBoolean(key, ObjectJudge.isTrue(value));
        } else if (value instanceof boolean[]) {
            //boolean[]
            bundle.putBooleanArray(key, (boolean[]) value);
        } else if (value instanceof Byte) {
            //byte
            bundle.putByte(key, (Byte) value);
        } else if (value instanceof Character) {
            //character
            bundle.putChar(key, (char) value);
        } else if (value instanceof Double) {
            //double
            bundle.putDouble(key, (Double) value);
        } else if (value instanceof double[]) {
            //double[]
            bundle.putDoubleArray(key, (double[]) value);
        } else if (value instanceof Float) {
            //float
            bundle.putFloat(key, (Float) value);
        } else if (value instanceof float[]) {
            //float[]
            bundle.putFloatArray(key, (float[]) value);
        } else if (value instanceof Integer) {
            //integer
            bundle.putInt(key, (Integer) value);
        } else if (value instanceof int[]) {
            //Integer[]
            bundle.putIntArray(key, (int[]) value);
        } else if (value instanceof Long) {
            //long
            bundle.putLong(key, (Long) value);
        } else if (value instanceof long[]) {
            //long[]
            bundle.putLongArray(key, (long[]) value);
        } else if (value instanceof Short) {
            //short
            bundle.putShort(key, (Short) value);
        } else if (value instanceof short[]) {
            //short
            bundle.putShortArray(key, (short[]) value);
        } else if (value instanceof String) {
            //string
            bundle.putString(key, (String) value);
        } else if (value instanceof String[]) {
            //string[]
            bundle.putStringArray(key, (String[]) value);
        } else if (value instanceof ArrayList) {
            //ArrayList
            ArrayList lst = (ArrayList) value;
            if (ObjectJudge.isNullOrEmpty(lst)) {
                return;
            }
            Object o = lst.get(0);
            if (o instanceof Integer) {
                bundle.putIntegerArrayList(key, lst);
            } else if (o instanceof String) {
                bundle.putStringArrayList(key, lst);
            } else if (o instanceof Parcelable) {
                bundle.putParcelableArrayList(key, lst);
            } else if (o instanceof CharSequence) {
                bundle.putCharSequenceArrayList(key, lst);
            }
        } else if (value instanceof Parcelable[]) {
            //Parcelable[]
            bundle.putParcelableArray(key, (Parcelable[]) value);
        } else if (value instanceof Parcelable) {
            //Parcelable
            bundle.putParcelable(key, (Parcelable) value);
//            bundle.putBinder();
        } else if (value instanceof char[]) {
            //Character[]
            bundle.putCharArray(key, (char[]) value);
        } else if (value instanceof CharSequence) {
            //CharSequence
            bundle.putCharSequence(key, (CharSequence) value);
        } else if (value instanceof CharSequence[]) {
            //CharSequence[]
            bundle.putCharSequenceArray(key, (CharSequence[]) value);
        } else if (value instanceof Serializable) {
            //Serializable
            bundle.putSerializable(key, (Serializable) value);
        } else if (value instanceof byte[]) {
            //byte[]
            bundle.putByteArray(key, (byte[]) value);
        } else if (value instanceof IBinder) {
            //IBinder
            bundle.putBinder(key, (IBinder) value);
        } else if (value instanceof SparseArray) {
            //SparseArray
            bundle.putSparseParcelableArray(key, (SparseArray) value);
        } else {
            if (value instanceof Size) {
                bundle.putSize(key, (Size) value);
            } else if (value instanceof SizeF) {
                bundle.putSizeF(key, (SizeF) value);
            }
        }
    }

    /**
     * 合并bundle
     *
     * @param source 要合并的bundle
     * @param target 合并到的bundle
     */
    public static void mergeBundle(Bundle source, Bundle target) {
        if (source == null || source.isEmpty() || target == null) {
            return;
        }
        for (String key : source.keySet()) {
            Object value = source.get(key);
            setBundleValue(target, key, value);
        }
    }
}
